package com.xxx.jdbc;

import com.alibaba.druid.pool.DruidDataSource;
import javafx.beans.property.Property;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @version v1.0
 * @Description:
 * @Author zhanzhongxin
 * @Date 2021/8/17 9:35
 * @Copyright 博为峰
 */
//@Component("jdbcConfig")
@Configuration
@PropertySource("druid.properties")
public class JdbcConfig {

    @Value("${jdbc.driver}")
    public String driver;
    @Value("${jdbc.url}")
    public String url;
    @Value("${jdbc.username}")
    public String username;
    @Value("${jdbc.password}")
    public String password;

    @Bean("conn")
    public Connection getConnection(@Qualifier("dataSource") DataSource dataSource) throws SQLException {
        return dataSource.getConnection();
    }

    @Bean(name = "dataSource")
    public DataSource getDataSource() {
        final DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}
